<?php

namespace App\Admin\Controllers;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopCategoryLogic;
use App\Models\ImportPlugModel;
use App\Models\SystemConfigModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Illuminate\Support\Arr;

class ImportPlugController extends AdminController
{
    protected $title = '批量上传';

    protected function grid(): Grid
    {
        $grid = new Grid(new ImportPlugModel);
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id', 'ID')->integer();
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', __('l.status'))->select(__('import_plug.statusArr'));
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->disableCreateButton(false);
        $grid->disableActions(false);
        $grid->actions(function (Actions $actions) {
            $actions->disableEdit(false);
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * form
     * @return Form
     */
    public function form(): Form
    {
        $form = new Form(new ImportPlugModel);
        $form->hidden('serial_number');
        $form->column(1 / 2, function (Form $form) {
            $form->file('file', '文件')->rules('mimes:zip')->move('zip', uniqid() . '.zip')->required();
            $form->select('category_id', '类目')->default(3)->options(ShopCategoryLogic::getByParentId(1))
                ->load('sub_category_id', '/admin/ajax/category')->required()->rules('required');
            $form->number('original_price', '原价')->default(1)->required()->rules('required|integer|min:1');
            $form->datetime('discount_at', '优惠时间')->default(now()->addDays()->startOfDay()->toDateTimeString())
                ->rules('required|date|date_format:Y-m-d H:i:s|after:' . now()->addHours()->toDateTimeString())->required();
        });
        $form->column(1 / 2, function (Form $form) {
            $config = SystemConfigModel::query()->where('id',1)->value('content');
            $classList = Arr::get($config, 'jlTag');
            //$classList = array_column($classList,'contet');
            $form->select('plug', __('shop_product.plug'))/*->default(1)*/->options($classList);
            $form->select('sub_category_id', '子类目')->default(7)->options(ShopCategoryLogic::getByParentId(3))
                ->required()->rules('required');
            $form->number('price', '优惠价')->default(1)->required()->rules('required|integer|min:1');
        });
        $form->saving(function (Form $form) {
            $form->serial_number = serialNumber();
        });
        $form->saved(function (Form $form) {
            mq('importPlug', $form->serial_number);
            sleep(2);
        });
        OperateLogLogic::add(Admin::user()->user_id, 'admin.plug.import');
        return $form->setWidth(10)->confirm('确定提交吗?');
    }
}
